﻿//-----------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="FER">
//     Copyright (c) All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace NMBP.View.ViewModel
{
    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;
    using NMBP.DAL;
    using NMBP.Models;
    using NMBP.ViewModels.Dialogs;

    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Inputed username
        /// </summary>
        private string username;

        /// <summary>
        /// Inputed password
        /// </summary>
        private string password;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            this.DialogService = new DialogService();

            this.LogInCommand = new RelayCommand(() =>
                {
                    if (this.DbFacade == null)
                    {
                        this.DbFacade = new DbFacade();
                    }

                    if (this.Username != null && this.Password != null)
                    {
                        var authenticatedUser = DbFacade.GetPerson(this.Username, this.Password);
                        if (authenticatedUser == null)
                        {
                            DialogService.ShowMessage("Neuspješna autorizacija! Krivo korisničko ime ili lozinka.", MessageType.ERROR);
                        }
                        else
                        {
                            if (authenticatedUser is Customer)
                            {
                                Messenger.Default.Send<Customer>(authenticatedUser as Customer, "MainSentCustomer");
                            }
                            else if (authenticatedUser is Worker)
                            {
                                Messenger.Default.Send<Worker>(authenticatedUser as Worker, "MainSentWorker");
                            }
                        }
                    }
                    else
                    {
                        DialogService.ShowMessage("Unesite korisničko ime i lozinku!", MessageType.ERROR);
                    }
                });
        }

        /// <summary>
        /// Gets or sets dialog service (interface for messagebox)
        /// </summary>
        public IDialogService DialogService { get; set; }

        /// <summary>
        /// Gets or sets filled in username
        /// </summary>
        public string Username
        {
            get
            {
                return this.username;
            }

            set
            {
                this.username = value;
                this.RaisePropertyChanged("Username");
            }
        }

        /// <summary>
        /// Gets or sets filled in password
        /// </summary>
        public string Password
        {
            get
            {
                return this.password;
            }

            set
            {
                this.password = value;
                this.RaisePropertyChanged("Password");
            }
        }

        /// <summary>
        /// Gets command executed when save button is clicked
        /// </summary>
        public RelayCommand LogInCommand { get; private set; }

        /// <summary>
        /// Gets or sets instance of database facade
        /// </summary>
        public IDbFacade DbFacade { get; set; }
    }
}